package com.atguigu.gulimall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuReductionTo;
import com.atguigu.gulimall.coupon.entity.MemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SkuLadderEntity;
import com.atguigu.gulimall.coupon.service.MemberPriceService;
import com.atguigu.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SkuFullReductionDao;
import com.atguigu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //保存阶梯价信息
        SkuLadderEntity skuLadderEntity = BeanUtil.copyProperties(skuReductionTo, SkuLadderEntity.class);
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        if (skuLadderEntity.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }
        //保存满减信息
        SkuFullReductionEntity skuFullReductionEntity = BeanUtil.copyProperties(skuReductionTo, SkuFullReductionEntity.class);
        skuFullReductionEntity.setAddOther(skuReductionTo.getCountStatus());
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            this.save(skuFullReductionEntity);
        }

        //保存会员价信息
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (memberPrice != null && memberPrice.size() != 0) {
            List<MemberPriceEntity> collect = memberPrice.stream().map(memberPrice1 -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setMemberLevelId(memberPrice1.getId());
                memberPriceEntity.setMemberLevelName(memberPrice1.getName());
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setAddOther(1);
                memberPriceEntity.setMemberPrice(memberPrice1.getPrice());
                return memberPriceEntity;
            }).filter(item->item.getMemberPrice().compareTo(new BigDecimal("0"))==1).collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }
    }
}